package com.jt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import com.jt.pojo.Node;
import com.jt.service.ItemCatService;
import com.jt.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    ItemCatMapper itemCatMapper;
    /**
     * 业务说明：查找所有的商品子树集合
     * */
    @Override
    public List<ItemCat> getItemCatList() {
        return itemCatMapper.selectList(null);
    }

    @Override
    public String getItemCatNameById(Integer id) {
        return itemCatMapper.selectById(id).getName();
    }

    @Override
    public List<Node> getItemCatNodeById(Long id) {
        id=id==null?0:id;
        QueryWrapper<ItemCat> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<ItemCat> itemCatList=itemCatMapper.selectList(queryWrapper);
        List<Node> nodeList=new ArrayList<>();
        for(ItemCat itemCat:itemCatList){
            Node node=new Node().setId(itemCat.getId()).setText(itemCat.getName());
            if(itemCat.getIsParent()){
                node.setState("closed");
            }else {
                node.setState("open");
            }
            nodeList.add(node);
        }
        return nodeList;
    }
}
